package cn.wenhaha.job.config;

import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Properties;

/**
 * quartz工厂
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-10-26 19:31
 */
@Configuration
public class ScheduleConfig {

    @Bean(name = "schedulerFactory")
    public JobFactory jobFactory(AutowireCapableBeanFactory autowireCapableBeanFactory){
        return new SchedulerFactory(autowireCapableBeanFactory);
    }

    @Bean(name = "schedulerConfigProperties")
    public Properties schedulerConfig(){
        // quartz参数
        Properties prop = new Properties();
        prop.put("org.quartz.scheduler.instanceName","SYNC_SCHEDULER");
        prop.put("org.quartz.scheduler.instanceId","AUTO");
        // 线程池配置
        prop.put("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "50");
        prop.put("org.quartz.threadPool.threadPriority","5");
        // JobStore配置
        prop.put("org.quartz.jobStore.class", "org.springframework.scheduling.quartz.LocalDataSourceJobStore");
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        // 集群配置
        prop.put("org.quartz.jobStore.isClustered", "true");
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");
        prop.put("org.quartz.jobStore.acquireTriggersWithinLock", "true");

        // 延迟多久后末执行，就代表失败
        prop.put("org.quartz.jobStore.misfireThreshold", "1000");
        // 表前缀
        prop.put("org.quartz.jobStore.tablePrefix", "SYNC_");
        return  prop;
    }


    @Bean
    public  SchedulerFactoryBean schedulerFactory(){
        return new SchedulerFactoryBean();
    }

}
